package com.zyl.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author zhaoyl
 * @date 2024/5/16 17:51
 * @description 说明
 * ArrayBlockingQueue队列的添加删除四种状态
 */
public class BlockQueueTest {
    public static void main(String[] args) throws InterruptedException {
        //testArrayBlockingQueue();
       // testArrayBlockingQueue2();
       // testArrayBlockingQueue3();
        testArrayBlockingQueue4();
    }

    //为空抛出异常（add，remove）
    public  static void testArrayBlockingQueue(){
        //一个由循环数组支持的有界阻塞队列。它的本质是一个基于数组的BlockingQueue的实现。 它的容纳大小是固定的。
        // 此队列按 FIFO（先进先出）原则对元素进行排序。 队列的头部 是在队列中存在时间最长的元素。
        // 队列的尾部 是在队列中存在时间最短的元素。
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);//初始化大小为3

        //添加
        System.out.println(blockingQueue.add("a-1"));//true
        System.out.println(blockingQueue.add("b-2"));//true
        System.out.println(blockingQueue.add("c-3"));//true

        // 抛出时异常：java.lang.IllegalStateException: Queue full
        //  System.out.println(blockingQueue.add("d-4"));//超出队列大小add()报错

        //查看队首原数element()，没有则抛出异常
        System.out.println(blockingQueue.element());//a-1

        System.out.println("========取出==========");
        //取出
        System.out.println(blockingQueue.remove());//a-1
        System.out.println(blockingQueue.remove());//b-2
        System.out.println(blockingQueue.remove());//c-3
        //抛出异常：java.util.NoSuchElementException
        System.out.println(blockingQueue.remove());//当队列为空remove()是抛出异常
    }
    //返回null,不抛异常（offer，poll）
    public  static void testArrayBlockingQueue2(){
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);//初始化大小为3
        System.out.println(blockingQueue.offer("a-1"));//true
        System.out.println(blockingQueue.offer("b-2"));//true
        System.out.println(blockingQueue.offer("c-3"));//true
        //当超出队列大小时offer()返回false
        System.out.println(blockingQueue.offer("d-4"));//false,添加失败

        //查看队首原数element()，没有返回null
        System.out.println(blockingQueue.peek());//a-1

        System.out.println("========取出==========");
        System.out.println(blockingQueue.poll());//a-1
        System.out.println(blockingQueue.poll());//b-2
        System.out.println(blockingQueue.poll());//c-3
        //当队列为空poll()返回null
        System.out.println(blockingQueue.poll());//null
    }
    //等待，一直阻塞（put，take）
    public  static void testArrayBlockingQueue3() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);//初始化大小为3
        //添加，没有返回值
        blockingQueue.put("a-1");
        blockingQueue.put("b-2");
        blockingQueue.put("c-3");
        //blockingQueue.put("d-3");//会一直阻塞
        System.out.println("========取出==========");
        System.out.println(blockingQueue.take());//a-1
        System.out.println(blockingQueue.take());//b-2
        System.out.println(blockingQueue.take());//c-3
       // System.out.println(blockingQueue.take());//没有一直等待阻塞
    }
    //等待，阻塞：等待超时退出 offer(E e, long timeout, TimeUnit unit)
    public  static void testArrayBlockingQueue4() throws InterruptedException {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);//初始化大小为3
        System.out.println(blockingQueue.offer("a-1"));//true
        System.out.println(blockingQueue.offer("b-2"));//true
        System.out.println(blockingQueue.offer("c-3"));//true
        //当超出队列大小时offer(E e, long timeout, TimeUnit unit)返回false 超过2秒退出
        System.out.println(blockingQueue.offer("d-4",2,TimeUnit.SECONDS));


        System.out.println("========取出==========");
        System.out.println(blockingQueue.poll());//a-1
        System.out.println(blockingQueue.poll());//b-2
        System.out.println(blockingQueue.poll());//c-3
        //当队列为空poll(long timeout, TimeUnit unit)超时退出
        System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));
    }
}
